import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '/base/view.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'logic.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class InventoryPage extends StatelessWidget {
  final logic = Get.put(InventoryLogic());
  final state = Get.find<InventoryLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFEFF5FC),
      appBar: getAppBar(),
      body: const MyBody(),
    );
  }

  AppBar getAppBar() {
    return AppBar(
      backgroundColor: Colors.white,
      elevation: 0,
      leading: GestureDetector(
        child: Padding(
          padding: EdgeInsets.symmetric(horizontal: 8.w),
          child: Image.asset(
            "images/ic_back.png",
            width: 50.w,
            height: 34.w,
          ),
        ),
        onTap: () {
          Get.back();
        },
      ),
      actions: [
        Align(
            alignment: Alignment.centerRight,
            child: Padding(
              padding: EdgeInsets.symmetric(horizontal: 16.w),
              child: Text(
                "新增",
                style: TextStyle(fontSize: 16.w, color: Color(0xff333333)),
              ),
            ))
      ],
      centerTitle: true,
      title: Text(
        "库存盘点",
        style: TextStyle(fontSize: 18.w, color: Color(0xff333333)),
      ),
    );
  }
}

class MyBody extends StatefulWidget {
  const MyBody({Key? key}) : super(key: key);

  @override
  State<MyBody> createState() => _MyBodyState();
}

class _MyBodyState extends State<MyBody> {
  List<String> items = [];
  final RefreshController _controller = RefreshController(initialRefresh: false);

  @override
  void initState() {
    super.initState();
    for (int i = 1; i <= 30; i++) {
      items.add(i.toString());
    }
  }

  @override
  Widget build(BuildContext context) {
    return getBody();
  }

  Widget getBody() {
    return SmartRefresher(
      enablePullDown: true,
      enablePullUp: true,
      controller: _controller,
      child: ListView.builder(
          itemCount: items.length,
          itemBuilder: (cxt, index) {
            return getListItem(items[index]);
          }),
      onRefresh: () async {
        items.clear();
        for (int i = 1; i <= 30; i++) {
          items.add(i.toString());
        }
        await Future.delayed(const Duration(milliseconds: 1000));
        _controller.refreshCompleted();
        _controller.loadComplete();
        setState(() {});
      },
      onLoading: () async {
        final count = items.length;
        for (int i = count + 1; i <= count + 20; i++) {
          items.add(i.toString());
        }
        await Future.delayed(const Duration(milliseconds: 1000));
        _controller.loadComplete();
        // if (items.length >= 70) {
        //   _controller.loadNoData();
        // } else {
        //   _controller.loadComplete();
        // }
        setState(() {});
      },
      footer: CustomFooter(
        builder: (BuildContext context,LoadStatus? mode){
          Widget body;
          if(mode==LoadStatus.idle){
            body =  const Text("上拉加载");
          }
          else if(mode==LoadStatus.loading){
            body = Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: const [
                CupertinoActivityIndicator(),
                SizedBox(width: 10),
                Text("加载中")
              ],
            );
          }
          else if(mode == LoadStatus.failed){
            body = const Text("加载失败！点击重试！");
          }
          else if(mode == LoadStatus.canLoading){
            body = const Text("松手,加载更多!");
          }
          else{
            body = const Text("没有更多数据了!");
          }
          return Container(
            height: 55.0,
            child: Center(child:body),
          );
        },
      ),
    );
  }

  Widget getListItem(String item) {
    return Container(
      height: 82.w,
      margin: EdgeInsets.only(top: 16.w, left: 16.w, right: 16.w),
      decoration: BoxDecoration(
          color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
      child: Row(
        children: [
          SizedBox(
            width: 12.w,
          ),
          Expanded(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  "PD2021013000${item}",
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(fontSize: 16.w, color: Color(0xFF333333)),
                ),
                SizedBox(
                  height: 8.w,
                ),
                Text("20$item库房",
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style:
                    TextStyle(fontSize: 14.w, color: Color(0xFF666666))),
              ],
            ),
          ),
          Container(
              margin: EdgeInsets.symmetric(horizontal: 12.w),
              padding: EdgeInsets.symmetric(vertical: 2.w, horizontal: 5.w),
              decoration: BoxDecoration(
                  color: Color(0xFFFFF4EC),
                  borderRadius: BorderRadius.circular(4.w)),
              child: Text(
                "待盘点",
                style: TextStyle(fontSize: 14.w, color: Color(0xFFEE8710)),
              ))
        ],
      ),
    );
  }
}
